package com.bbs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bbs.biz.UserBiz;
import com.bbs.biz.impl.UserBizImpl;
import com.bbs.model.Posts;
import com.bbs.model.User;

public class PostReleaseServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String author = (String) request.getSession().getAttribute("username");

		String posttitle = request.getParameter("back_post_release_title");
		String postkind = request.getParameter("selectHelpCode");
		String postcontent = request.getParameter("cont");

		HttpSession session = request.getSession();

		if (posttitle.trim() != "" && postkind.trim() != ""
				&& postcontent.trim() != "") {
			Posts post = new Posts();
			post.setTitle(posttitle);
			post.setKind(postkind);
			post.setContent(postcontent);
			post.setAuthor(author);
			post.setTime(new Date());
			UserBiz biz = new UserBizImpl();
			try {
				if (biz.postRelease(post)) {
					request.setAttribute("release", "发布成功！");

					RequestDispatcher dispatcher = request
							.getRequestDispatcher("back_post_release.jsp");
					dispatcher.forward(request, response);
				} else {
					session.setAttribute("release", "标题或是帖子类别和内容缺失，发布失败！");
					// response.sendRedirect("back_post_release.jsp");
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("back_post_release.jsp");
					dispatcher.forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			session.setAttribute("error1", "不能为空！");
			response.sendRedirect("back_post_release.jsp");
		}

	}

}
